Ext.ns('Tm.company');
Tm.company.CompanyContactorPanel = Ext.extend(Ext.Panel, {
			id:'companyContactorPanel',
			compId : null,
			frame : true,
			autoWidth : true,
			
			layout : 'fit',
			initComponent : function() {
				var gridId = Ext.id();
				var compId = this.compId;
				var sm=new Ext.grid.CheckboxSelectionModel();
				var cm = new Ext.grid.ColumnModel([ sm,{
							header : 'id',
							width : 20,
							dataIndex : 'id',
							hidden : true,
							renderer:   function (data, metadata, record, rowIndex, columnIndex, store) {  
							    metadata.attr = 'ext:qtitle="' + this.header+ '"' + ' ext:qtip="' + data + '"';  
							    return data;  
							}
						}, {
							header : '姓名',
							width : 80,
							dataIndex : 'name',
							sortable : true,
							renderer:   function (data, metadata, record, rowIndex, columnIndex, store) {  
							    metadata.attr = 'ext:qtitle="' + this.header+ '"' + ' ext:qtip="' + data + '"';  
							    return data;  
							}
						}, {
							header : '职务',
							width : 80,
							dataIndex : 'jobtitle',
							sortable : true,
							renderer:   function (data, metadata, record, rowIndex, columnIndex, store) {  
							    metadata.attr = 'ext:qtitle="' + this.header+ '"' + ' ext:qtip="' + data + '"';  
							    return data;  
							}
						}, {
							header : '办公电话',
							width : 100,
							dataIndex : 'tel',
							sortable : true,
							editor : {
								xtype : 'textfield',
								allowBlank : true
							},
							renderer:   function (data, metadata, record, rowIndex, columnIndex, store) {  
							    metadata.attr = 'ext:qtitle="' + this.header+ '"' + ' ext:qtip="' + data + '"';  
							    return data;  
							}
						}, {
							header : '移动电话',
							width : 100,
							dataIndex : 'mobile',
							sortable : true,
							renderer:   function (data, metadata, record, rowIndex, columnIndex, store) {  
							    metadata.attr = 'ext:qtitle="' + this.header+ '"' + ' ext:qtip="' + data + '"';  
							    return data;  
							}
						}, {
							header : '移动电话(2)',
							width : 100,
							dataIndex : 'standbyMobile',
							sortable : true,
							renderer:   function (data, metadata, record, rowIndex, columnIndex, store) {  
							    metadata.attr = 'ext:qtitle="' + this.header+ '"' + ' ext:qtip="' + data + '"';  
							    return data;  
							}
						}
						
						, {
							header : '传真',
							width : 100,
							dataIndex : 'fax',
							sortable : true,
							renderer:   function (data, metadata, record, rowIndex, columnIndex, store) {  
							    metadata.attr = 'ext:qtitle="' + this.header+ '"' + ' ext:qtip="' + data + '"';  
							    return data;  
							}
						}, {
							header : '邮箱',
							width : 100,
							dataIndex : 'email',
							sortable : true,
							renderer:   function (data, metadata, record, rowIndex, columnIndex, store) {  
							    metadata.attr = 'ext:qtitle="' + this.header+ '"' + ' ext:qtip="' + data + '"';  
							    return data;  
							}
						}, {
							header : '地址',
							width : 150,
							dataIndex : 'address',
							sortable : true,
							renderer:   function (data, metadata, record, rowIndex, columnIndex, store) {  
							    metadata.attr = 'ext:qtitle="' + this.header+ '"' + ' ext:qtip="' + data + '"';  
							    return data;  
							}
			
						}, {
							header : '默认',
							width : 50,
							dataIndex : 'isdefault',
							sortable : true,
							xtype : 'combocolumn',
							editor: new Ext.form.ComboBox({
								name : 'isdefault',
								mode : 'local',
								editable : false,
								typeAhead : true,
								forceSelection : true,
								hiddenName : 'isdefault',
								mode : 'local',
								triggerAction : 'all',
								selectOnFocus : true,
								store : Tm.data.contactorIsdefault,
								allowBlank : false
							})}]);
				cm.defaultSortable = true;
				var fields = [{
							name : 'id',
							type : 'int',
							mapping : 'id'
						}, {
							name : 'name',
							type : 'string',
							mapping : 'name'
						}, {
							name : 'jobtitle',
							type : 'string',
							mapping : 'jobtitle'
						}, {
							name : 'tel',
							type : 'string',
							mapping : 'tel'
						}, {
							name : 'mobile',
							type : 'string',
							mapping : 'mobile'
						}, {
							name : 'standbyMobile',
							type : 'string',
							mapping : 'standbyMobile'
						}, {
							name : 'fax',
							type : 'string',
							mapping : 'fax'
						}, {
							name : 'email',
							type : 'string',
							mapping : 'email'
						}, {
							name : 'address',
							type : 'string',
							mapping : 'address'
						}, {
							name : 'isdefault',
							type : 'string',
							mapping : 'isdefault'
						}];
				var ds = new Ext.data.Store({
							id : 'companyIndexPanelGridPanelStore',
							proxy : new Ext.data.HttpProxy({
										url : 'company/queryContactor.do'
									}),
							reader : new Ext.data.JsonReader({
										totalProperty : 'totalProperty',
										root : 'root',
										id : 'id',
										fields : fields
									}),
							baseParams : {
								start : 0,
								limit : 20
							},

							sortInfo : {
								field : 'id',
								direction : 'ASC'
							},
							autoLoad : compId==null?false:true,
							listeners : {
								beforeload : function() {
									Ext.apply(this.baseParams, {
												compId : Ext.getCmp('companyContactorPanel').compId
											});
								}
							}
						}

				);
//				var editor = new Ext.ux.grid.RowEditor({
//							// clicksToEdit : 2,
//							// this changes from the default double-click activation to single click activation
//							errorSummary : false,// disables display of validation messages if the row is invalid
//							listeners : {
//
//								afteredit : function(roweditor, changes, record, rowIndex) {
//									// console.dir(record);
//									Ext.Ajax.request({
//												url : 'company/saveOrUpdateContactor.do',
//												method : 'POST',
//												params : Ext.apply(record.data, {
//															compId : Ext.getCmp('companyContactorPanel').compId
//														}),
//												success : function() {
//													record.store.load();
//												}
//											});
//								},
//								canceledit : function(roweditor, pressed) {
//									// console.dir(roweditor);
//
//									if (!roweditor.record.data.id) {
//										roweditor.grid.getStore().remove(roweditor.record);
//									}
//
//								},
//
//								move : function(p) {
//									this.resize();
//								},
//								hide : function(p) {
//									var mainBody = this.grid.getView().mainBody;
//									var lastRow = Ext.fly(this.grid.getView().getRow(this.grid.getStore().getCount() - 1));
//									mainBody.setHeight(lastRow.getBottom() - mainBody.getTop(), {
//												callback : function() {
//													mainBody.setHeight('auto');
//												}
//											});
//								},
//								afterlayout : function(container, layout) {
//									this.resize();
//								}
//							},
//							resize : function() {
//								var row = Ext.fly(this.grid.getView().getRow(this.rowIndex)).getBottom();
//								var lastRow = Ext.fly(this.grid.getView().getRow(this.grid.getStore().getCount() - 1)).getBottom();
//								var mainBody = this.grid.getView().mainBody;
//								var h = Ext.max([row + this.btns.getHeight() + 10, lastRow]) - mainBody.getTop();
//								mainBody.setHeight(h, true);
//							}
//						});
				var grid = new Ext.grid.GridPanel({
							id : gridId,
							store : ds,
							sm : sm,
							cm : cm,
							region : 'center',
							border : false,
							stripeRows : true,
							autoHeight : true,
							autoExpandColumn : 'description',
							viewConfig : {
								forceFit : true
							},
							listeners : {
								 celldblclick : function(grid, rowIndex, columnIndex, event) {
											 var store = grid.getStore();
											 var data = store.getAt(rowIndex).data;
											
											 var contactorAddPanel = new Tm.company.ContactorAddPanel({dataId:data.id,compId:Ext.getCmp('companyContactorPanel').compId});
												var contactorAddWindow = new Tm.common.CommonWindow(
														{
															id : 'contactorAddWindow',
															title : '更新联系人',
															width : 450,
															height : 320,
															items : contactorAddPanel
														});
												contactorAddWindow.on(
														'close', function(panel) {
															ds.load();
														});
												contactorAddWindow.show();
											
											 }
											},
						//	plugins : [editor],
							bbar : new Ext.PagingToolbar({
										plugins : new Ext.ux.ProgressBarPager(),
										pageSize : 20,
										store : ds,
										displayInfo : true,
										emptyMsg : '没有记录'
									})
						});
				var toolBar = new Ext.Toolbar({
							hideBorders : true,
							buttons : [  {
										text : '新增',
										iconCls : 'act_addIcon',

										handler : function() {
											// access the Record constructor through the grid's store
//											var Contactor = grid.getStore().recordType;
//											var c = new Contactor({
//														isdefault : 'N'
//													});
//											if (grid.getStore().data.length == 0) {
//												c = new Contactor({
//															isdefault : 'Y'
//														});
//											}
//											editor.stopEditing();
//											grid.getStore().insert(0, c);
//											grid.getView().refresh();
//											grid.getSelectionModel().selectRow(0);
//											editor.startEditing(0);
											

													
											 var contactorAddPanel = new Tm.company.ContactorAddPanel({compId:Ext.getCmp('companyContactorPanel').compId});
											var contactorAddWindow = new Tm.common.CommonWindow(
													{
														id : 'contactorAddWindow',
														title : '新增联系人',
														width : 450,
														height : 320,
														items : contactorAddPanel
													});
											contactorAddWindow.on(
													'close', function(panel) {
														ds.load();
													});
											contactorAddWindow.show();
										
											
											
											
											
										}
									}, {
										xtype : 'tbseparator'
									}, {
										text : '删除',
										iconCls : 'act_deleteIcon',

										handler : function() {
											var records = grid.getSelectionModel().selections.items;
											var ids=[];
											if (records.length == 0) {
												return;
											} else  {
												var breakException='break';
												try {
													records.forEach(function(record, index, array){
	 													if(record.data.isdefault=='Y'){
	 														App.setAlert('notice', '默认联系人无法删除，请更换默认联系人再进行此操作。');
															throw breakException;
 														}else{
															ids.push(record.data.id);
														}
													});
														
												} catch(e) {
												    if (e!==breakException) {
												    	throw e;
												    }else{
												    	return;
												    }
												}

															
											} 
												Ext.MessageBox.confirm('注意', '您确定要进行该操作？', function(btn) {

															if (btn == 'yes') {

																Ext.Ajax.request({
																			url : 'company/deleteContactors.do',
																			method : 'POST',
																			params : {
																				ids : ids
																			},
																			success : function(action, response) {
																				grid.getStore().load();
																			},
																			failure : function(action, response) {
																				App.setAlert('error', '操作失败');

																			}
																		});
															}

														});

											
										}
									}, {
										xtype : 'tbseparator'
									}, {
										text : '默认联系人',
										iconCls : 'act_setdefaultIcon',
										handler : function() {
											//editor.stopEditing();
											var ids = grid.getSelectionModel().selections.keys;
											if (ids.length == 0) {
											} else if (ids.length > 1) {
												App.setAlert('notice', '一家客户只允许设置一位默认联系人。');
											} else {
												Ext.Ajax.request({
															url : 'company/setDefaultContactor.do',
															method : 'POST',
															params : {
																id : ids[0]
															},
															success : function(action, response) {
																App.setAlert('ok', '操作成功');
																grid.getStore().load();
															},
															failure : function(action, response) {
																App.setAlert('error', '操作失败');
															}
														});
											}
										}
									}]
						});
				Ext.apply(this, {
							tbar : toolBar,
							items : [grid]
						});
				Tm.company.CompanyContactorPanel.superclass.initComponent.call(this);
			}
		});
